package com.gly091020.ohMyPC;

import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.neoforged.fml.loading.FMLPaths;

import java.util.Objects;

@Config(name = OhMyPC.ModID)
public class OhMyPCConfig implements ConfigData {
    public boolean enable = true;
    public int fps = 24;
    public int maxTime = 50;
    public int notRespondingTime = 1000;
    public boolean playOnCrash = true;
    public String sound = SoundLoader.SOUNDS.getLast().getFileName().toString();

    public static Screen getConfigScreen(Screen parent){
        ConfigBuilder builder = ConfigBuilder.create()
                .setParentScreen(parent)
                .setTitle(Component.translatable("config.oh_my_pc.title"));
        ConfigCategory general = builder.getOrCreateCategory(Component.translatable("config.oh_my_pc.title"));
        ConfigEntryBuilder entryBuilder = builder.entryBuilder();
        general.addEntry(entryBuilder.startBooleanToggle(
                Component.translatable("config.oh_my_pc.enable"), OhMyPC.CONFIG.enable)
                        .setDefaultValue(true).setSaveConsumer(aBoolean -> OhMyPC.CONFIG.enable = aBoolean)
                .build());
        general.addEntry(entryBuilder.startIntSlider(Component.translatable("config.oh_my_pc.fps"),
                        OhMyPC.CONFIG.fps, 0, 30)
                        .setDefaultValue(24).setTooltip(Component.translatable("config.oh_my_pc.fps.tip"))
                .setSaveConsumer(integer -> OhMyPC.CONFIG.fps = integer)
                        .setTextGetter(integer -> Component.literal(Objects.toString(integer)).append(" Fps"))
                .build());
        general.addEntry(entryBuilder.startIntSlider(Component.translatable("config.oh_my_pc.max_time"),
                OhMyPC.CONFIG.maxTime, 1, 3000)
                        .setDefaultValue(50)
                        .setTooltip(Component.translatable("config.oh_my_pc.max_time.tip"))
                        .setTextGetter(integer -> Component.translatable("config.oh_my_pc.ms", integer))
                        .setSaveConsumer(integer -> OhMyPC.CONFIG.maxTime = integer)
                .build());
        general.addEntry(entryBuilder.startIntSlider(Component.translatable("config.oh_my_pc.not_responding_time"),
                        OhMyPC.CONFIG.notRespondingTime, 1, 10000)
                .setDefaultValue(1000)
                .setTooltip(Component.translatable("config.oh_my_pc.not_responding_time.tip"))
                .setTextGetter(integer -> Component.translatable("config.oh_my_pc.ms", integer))
                .setSaveConsumer(integer -> OhMyPC.CONFIG.notRespondingTime = integer)
                .build());
        general.addEntry(entryBuilder.startBooleanToggle(
                        Component.translatable("config.oh_my_pc.play_on_crash"), OhMyPC.CONFIG.playOnCrash)
                .setDefaultValue(true)
                .setSaveConsumer(aBoolean -> OhMyPC.CONFIG.playOnCrash = aBoolean)
                .build());
        general.addEntry(entryBuilder.startStringDropdownMenu(Component.translatable("config.oh_my_pc.sound"),
                        OhMyPC.CONFIG.sound, s -> {
                            var i = s.indexOf(".");
                            if(i < 0){
                                return Component.literal(s);
                            }
                            return Component.literal(s.substring(0, i));
                        })
                        .setDefaultValue("oh_my_pc")
                        .setErrorSupplier(s -> FMLPaths.CONFIGDIR.get().resolve("oh_my_pc")
                                .resolve(s + ".wav").toFile().isFile()
                                ? java.util.Optional.empty() : java.util.Optional.of(
                                        Component.translatable("config.oh_my_pc.sound.no_file")))
                        .setSelections(SoundLoader.SOUNDS.stream().map((p)-> p.getFileName().toString()).toList())
                        .setSaveConsumer(s -> OhMyPC.CONFIG.sound = s + ".wav")
                .build());
        if(Minecraft.getInstance().getUser().getProfileId().equals(OhMyPC._5112151111121)){
            general.addEntry(entryBuilder.startTextDescription(Component.literal(
                    Minecraft.getInstance().getUser().getName())
                            .append("，你又在打电动哦，休息一下好不好？"))
                    .build());
        }
        builder.setSavingRunnable(() -> {
            AutoConfig.getConfigHolder(OhMyPCConfig.class).setConfig(OhMyPC.CONFIG);
            AutoConfig.getConfigHolder(OhMyPCConfig.class).save();
            OhMyPC.CONFIG = AutoConfig.getConfigHolder(OhMyPCConfig.class).getConfig();
            SoundLoader.SOUND = FMLPaths.CONFIGDIR.get().resolve("oh_my_pc").resolve(OhMyPC.CONFIG.sound);
            SoundLoader.init();
        });
        return builder.build();
    }
}
